home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / shell.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  13KB  |  331 lines

  1. /* shell.c contains the routines to allow exec calls.  If there were a
  2.  * small command interpreter somewhere, we could do a real spawn.
  3.  * If you compile this to run from a shell, undefine GEMLOAD.  Since
  4.  * shells eat memory, you can't give as much back to the OS.  If you
  5.  * will be using this from GEM, give back 256K and you'll be able to
  6.  * run just about any program from within the editor.
  7.  */
  8.  
  9. #include "shell.h"
  10.  
  11. #ifdef  GEMLOAD
  12. unsigned long _STKSIZ = -262144;        /* give back 256K to the OS */
  13. #else
  14. unsigned long _STKSIZ = -147456;        /* give back 144K to the OS */
  15. #endif
  16.  
  17. char cline[MAXINPUT * 2];       /* holds the command line and other things */
  18. char pnam[MAXINPUT+1];          /* program name */
  19. char source[MAXINPUT+1];        /* name of file for aliasing */
  20. char path[MAXPATH+1];           /* current path string  */
  21. int delete = TRUE;              /* on/off for above     */
  22. int dolink = 0;                 /* link a program       */
  23. int temdrv;
  24. int curdrv;
  25. ALITAB *aheadp = NULL;          /* alias header */
  26.  
  27. shell(f, n)
  28. register int f, n;
  29. {
  30.         extern char *alias(), *index(), *rindex();
  31.         register char *ptr;
  32.  
  33.         if ((f=mlreply("!: ",cline,MAXINPUT))!=TRUE)
  34.                 return(f);
  35.         if ((ptr=index(cline,' '))!=(char *)NULL)
  36.                 {
  37.                 *ptr = '\0';
  38.                 strcpy(pnam,cline);
  39.                 strcpy(&cline[1],++ptr);
  40.                 }
  41.         else
  42.                 {
  43.                 strcpy(pnam,cline);
  44.                 strcpy(&cline[1]," ");
  45.                 }
  46.         if (pnam[0] == '~')
  47.                 parsefn(pnam);
  48.         if (alias(source,pnam)!=(char *)NULL)
  49.                 strcpy(pnam,source);
  50.         /* check for file type (prg) and append if not there */
  51.         if ((ptr=rindex(pnam,'\\'))!=(char *)NULL)      /* don't be fooled */
  52.                 {                                       /* by \foo.dir\bar */
  53.                 if (!index(ptr,'.'))
  54.                         strcat(ptr,".prg");
  55.                 }
  56.         else if (rindex(pnam,'.')==(char *)NULL)
  57.                 strcat(pnam,".prg");
  58.         if (cline[1] == '~')
  59.                 parsefn(&cline[1]);
  60.         while((ptr=index(&cline[1], '~'))!=(char *)NULL)
  61.                 if (!parsefn(ptr))
  62.                         break;
  63.         cline[0] = (char)strlen(&cline[1]);
  64.         ttclose();      /* reset terminal characteristics */
  65.         ttputc(0x1b); ttputc('E');
  66.         Setexc(2,buserr);       /* use default exception handlers */
  67.         Setexc(3,adderr);
  68.         f = (int)Pexec(0,pnam,cline,0L);
  69.         Setexc(2,&errexit);     /* reset local handlers */
  70.         Setexc(3,&errexit);
  71.         mtwrite("[Type a <CR>]");
  72.         ttgetc();
  73.         ttopen();       /* turn on uemail screen */
  74.         if (f == FALSE && n == HUGE)    /* called from term */
  75.                 ;
  76.         else
  77.                 {
  78.                 sgarbf = TRUE;  /* force full redraw */
  79.                 upmode();       /* set colors */
  80.                 update();       /* fix screen */
  81.                 }
  82.         mlwrite("Return from %s = %d",pnam,f);
  83.         return(TRUE);
  84. }
  85.  
  86. /* read in the cc.ini file if it's on the default drive.  Called once at
  87.  * editor startup and later by command.  Bound to CTRL-C.
  88.  */
  89. commfil(f,n)
  90. register int f,n;
  91. {
  92.         FILE *fopen();
  93.         register FILE *fp;
  94.         char *fgets(),*index();
  95.         char *ptr;
  96.         static char line[MAXINPUT+1];
  97.  
  98.         /* for the call from main */
  99.         if (f == FALSE && n == HUGE)
  100.                 {
  101.                 if ((fp=fopen("cc.ini","r")) == NULL)
  102.                         {
  103.                         Dgetpath(path,0);
  104.                         mlwrite("Cannot open %s\\cc.ini",path);
  105.                         return(FALSE);
  106.                         }
  107.                 }
  108.         else
  109.                 {
  110.                 if ((f=mlreply("Command file: ",line,MAXINPUT))!=TRUE)
  111.                         return(f);
  112.                 if (line[0] == '~')
  113.                         parsefn(line);
  114.                 if ((fp=fopen(line, "r"))==NULL)
  115.                         {
  116.                         mlwrite("Cannot open %s",line);
  117.                         return(FALSE);
  118.                         }
  119.                 }
  120.         while (fgets(line,MAXINPUT,fp))
  121.                 {
  122.                 /* kill trailing whitespace */
  123.                 if ((ptr=index(line,'\n'))!=(char *)NULL)
  124.                         *ptr = '\0';
  125.                 if ((ptr=index(line,' '))!=(char *)NULL)
  126.                         *ptr = '\0';
  127.                 if ((ptr=index(line,'\t'))!=(char *)NULL)
  128.                         *ptr = '\0';
  129.                 /* terminate alias and get address of path */
  130.                 if ((ptr=index(line,'='))!=(char *)NULL)
  131.                         *ptr = '\0';
  132.                 else    /* improper line */
  133.                         continue;
  134.                 ++ptr;  /* now equals &path[0] */
  135.                 /* test two cases that aren't easy in a switch */
  136.                 if (strcmp(line,"F10")==NULL)
  137.                         {
  138.                         bindkey((SPEC|'D'),ptr);
  139.                         continue;
  140.                         }
  141.                 if (strcmp(line,"NENTER")==NULL)
  142.                         {
  143.                         bindkey((SPEC|'r'),ptr);
  144.                         continue;
  145.                         }
  146.                 f = strlen(line);
  147.                 n = strlen(ptr);
  148.                 /* bind function keys */
  149.                 if (line[0] == 'F' && f == 2)
  150.                         {
  151.                         switch(line[1])
  152.                                 {
  153.                                 case '1':
  154.                                         bindkey((SPEC|';'),ptr);
  155.                                         break;
  156.                                 case '2':
  157.                                         bindkey((SPEC|'<'),ptr);
  158.                                         break;
  159.                                 case '3':
  160.                                         bindkey((SPEC|'='),ptr);
  161.                                         break;
  162.                                 case '4':
  163.                                         bindkey((SPEC|'>'),ptr);
  164.                                         break;
  165.                                 case '5':
  166.                                         bindkey((SPEC|'?'),ptr);
  167.                                         break;
  168.                                 case '6':
  169.                                         bindkey((SPEC|'@'),ptr);
  170.                                         break;
  171.                                 case '7':
  172.                                         bindkey((SPEC|'A'),ptr);
  173.                                         break;
  174.                                 case '8':
  175.                                         bindkey((SPEC|'B'),ptr);
  176.                                         break;
  177.                                 case '9':
  178.                                         bindkey((SPEC|'C'),ptr);
  179.                                         break;
  180.                                 default:
  181.                                         break;
  182.                                 }
  183.                         }
  184.                 /* or maybe a number pad key */
  185.                 else if (line[0] == 'N' && f == 2)
  186.                         {
  187.                         switch(line[1])
  188.                                 {
  189.                                 case '(':
  190.                                         bindkey((SPEC|'c'),ptr);
  191.                                         break;
  192.                                 case ')':
  193.                                         bindkey((SPEC|'d'),ptr);
  194.                                         break;
  195.                                 case '/':
  196.                                         bindkey((SPEC|'e'),ptr);
  197.                                         break;
  198.                                 case '*':
  199.                                         bindkey((SPEC|'f'),ptr);
  200.                                         break;
  201.                                 case '7':
  202.                                         bindkey((SPEC|'g'),ptr);
  203.                                         break;
  204.                                 case '8':
  205.                                         bindkey((SPEC|'h'),ptr);
  206.